﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Xml;
using Framework.Library.Components.Commands;
using Framework.Library.Components.Portal;
using Framework.Library.Utilities;
using Framework.Library;
using Framework.Library.Helper;
using System.Globalization;

namespace Framework.Library
{
	public class AppEnv
	{
        public const string DEFAULT_PAGE = "/Home/Default.aspx";        
        public const string FRAME_PAGE = "/Frame.aspx";
        public static string ACCESSDENY = CommandController.GetCommandbyID((int)PortalControls.AccessDeny);

        //co the dat cau hinh dong
		public static string ConnectionString
		{
			get 
            { 
                return WebConfigurationManager.ConnectionStrings["localsql"].ConnectionString; 
            }
		}
        public static string Portal
        {
            get
            {
                return (ConfigHelper.GetStringSetting("Portal", "/"));
            }
        }
        public static string GetDefaultLanguage()
        {
            return (ConfigHelper.GetStringSetting("DefaultLanguage", "vi-VN"));
        }
        public static string AllowedLanguage()
        {
            return (ConfigHelper.GetStringSetting("AllowedLanguage", "None"));
        }

        public static bool ForceTinyMCEInSafari
        {
            get { return ConfigHelper.GetBoolSetting("ForceTinyMCEInSafari", false); }
        }

        public static bool ForceTinyMCEInOpera
        {
            get { return ConfigHelper.GetBoolSetting("ForceTinyMCEInOpera", true); }
        }
        public static string ADMIN_CMD
        {
            get
            {
                return (ConfigHelper.GetStringSetting("ADMIN_CMD", "/Administrator/Default.aspx?lang=" + AppEnv.GetLanguage() + "&portalid=" + AppEnv.PortalId()));
            }
        }
        public static string DEFAULT_EXTENTSION
        {
            get
            {
                return (ConfigHelper.GetStringSetting("DEFAULT_EXTENTSION", ".html"));
            }
        }
        public static string ADMIN_EMAIL
        {
            get
            {
                return (ConfigHelper.GetStringSetting("MailAdmin", "mr.dungnq@ncs.com.vn"));
            }
        }
        public static int DEFAULT_ROLE
        {
            get
            {
                return ConfigHelper.GetIntSetting("DEFAULT_ROLE", 34);
            }
        }

        public static int GetTimeCacheExpire
        {
            get
            {
                return ConvertUtility.ToInt32(ConfigHelper.GetIntSetting("CacheExpire", 0));
            }
        }
        public static int CacheExpire_Zone
        {
            get { return ConvertUtility.ToInt32(ConfigHelper.GetIntSetting("CacheExpire_Zone", 3)); }
        }

        public static int CacheExpire_Content
        {
            get { return ConvertUtility.ToInt32(ConfigHelper.GetIntSetting("CacheExpire_Content", 3)); }
        }  

        public static string GetConnectionString(string name)
        {
            return WebConfigurationManager.ConnectionStrings[name].ConnectionString;
        }            	

		public static string GetAppSetting(string key, string configFileName)
		{
			XmlDocument xd = new XmlDocument();
			xd.Load(HttpContext.Current.Server.MapPath(configFileName));
			XmlNode node = xd.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key=\"" + key + "\"]");
			if (node != null) return node.Attributes.GetNamedItem("value").Value;
			else return null;
		}

        public static CultureInfo GetDefaultCulture()
        {
            try
            {
                string cultureString = GetDefaultLanguage();
                return new CultureInfo(cultureString);   
            }
            catch (ArgumentException) { }
            catch (System.Security.SecurityException) { }

            return CultureInfo.CurrentCulture;
        }

		public static void SetLanguage(string cultureCode)
		{
			HttpContext context = HttpContext.Current;
			HttpCookie cookieLang;
			cookieLang = new HttpCookie(Definition.Portal_Cookie, cultureCode);
			cookieLang.Expires = DateTime.Now.AddYears(100);
			context.Response.Cookies.Add(cookieLang);
		}

		public static string GetLanguage()
		{
			
			string strOutput = String.Empty;
			//if (HttpContext.Current.Request.QueryString["lang"] != null)
			//	strOutput = HttpContext.Current.Request.QueryString["lang"];
			//else 
			if (HttpContext.Current.Request.Cookies[Definition.Portal_Cookie] != null)
			{
				if (HttpContext.Current.Request.Cookies[Definition.Portal_Cookie].Value != string.Empty)
					strOutput = HttpContext.Current.Request.Cookies[Definition.Portal_Cookie].Value;
				else
				{
					SetLanguage(GetDefaultLanguage());
					strOutput = GetDefaultLanguage();
				}
			}
			else
			{
				SetLanguage(GetDefaultLanguage());
				strOutput = GetDefaultLanguage();
			}
			return strOutput;
		}		

        public static string AdminUrlParams(string cmd)
        {
            string lang = string.Empty;
            int portalid = 0;
            string srtLang = HttpContext.Current.Request["lang"];
            if (!String.IsNullOrEmpty(srtLang))
                lang = srtLang;
            else
                lang = AppEnv.GetLanguage();
            string strPortal = HttpContext.Current.Request["portalid"];
            if (!String.IsNullOrEmpty(strPortal))
                portalid = ConvertUtility.ToInt32(strPortal);
            else
                portalid = ConvertUtility.ToInt32(AppEnv.PortalId());

            return ADMIN_CMD + "?lang=" + lang + "&portalid=" + portalid + "&cmd=" + cmd;
        }

        public static string PortalId()
        {

            int portalId = ConvertUtility.ToInt32(HttpContext.Current.Request["portalid"]);
            if (portalId == 0)
            {
                portalId = ConvertUtility.ToInt32(PortalSetting.GetSetting()[Definition.Portal_Active + "_" + AppEnv.GetLanguage()]);
            }
            return portalId.ToString();
        }
        public static int CommandID()
        {
            return CommandController.GetCommand(ConvertUtility.ToString(HttpContext.Current.Request["cmd"])).CommandID;
        }

        public static string CommandName()
        {
            string cmdName = "";
            string cmd = ConvertUtility.ToString(HttpContext.Current.Request["cmd"]);
            if (cmd != null && cmd != "")
            {
                CommandInfo info = CommandController.GetCommand(cmd);
                cmdName = info.CommandName;
            }
            return cmdName;
        }

        public static string DateTimeNow()
        {
            if (DateTime.Now.ToString("ddd") != "cn")
            {
                if (GetLanguage() == "vi-VN")
                {
                    return "Thứ " + DateTime.Now.ToString("ddd") + ", " + DateTime.Now.ToString("dd/MM/yyyy");
                }
                else
                {
                    return DateTime.Now.ToString("ddd") + ", " + DateTime.Now.ToString("dd/MM/yyyy");
                }
            }
            else
            {
                if (GetLanguage() == "vi-VN")
                {
                    return "Chủ nhật, " + DateTime.Now.ToString("dd/MM/yyyy");
                }
                else
                {
                    return DateTime.Now.ToString("dd/MM/yyyy");

                }
            }
        }
	}
}
